package com.mc.web;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.mc.model.Path;
import com.mc.model.User;
import com.mc.model.newsandbulletin;
import com.mc.service.NewsandbulletService;
import com.mc.utils.PathUtil;
import com.mc.utils.StringUtil;

@Controller
@RequestMapping("/backend")
@SessionAttributes("user")
public class NewsAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NewsAction.class);
	private static String PREFIX = "";
	private Path path = null;
	
	@Autowired NewsandbulletService newsandbulletService;
	
	@RequestMapping("/addnewsui")
   public String addnewsui(ModelMap modelMap)
   {
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addnews","ViewTitle:添加新闻公告","smenu:newsandbullet","mmenu:addnews"});				
		return PathUtil.returnStr(path, modelMap);
   }
	
	@RequestMapping("/addnews")
   public String addnews(newsandbulletin nb,ModelMap modelMap,HttpSession session,HttpServletRequest request)
   {
		String path = request.getContextPath();
		String IP = "";
		//IP ip = new ipad
		InetAddress ia = null;
		try {
			ia = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}   
      //  String host = ia.getHostName();//获取计算机主机名  
         IP= ia.getHostAddress();//获取计算机IP  
		String basePath = path + "/ueditor/jsp/";
		
		//nb.setIndex(0);
		List<String>filepath = StringUtil.getInstace().getFilepaths();
	//	logger.info(filepath);
		
		String fp1 = "";
		for(String fp : filepath)
		{
			if(nb.getContent().contains(fp))
			{
				fp1 =  fp;
				nb.setUrl(basePath + fp1);
				nb.setIndex(1);
				break;
			}
		}
		
	//	logger.info(basePath + fp1 + "----url");
		nb.setAuthor((User)session.getAttribute("user"));
	   logger.info(nb.toString());
	   
	   newsandbulletService.insertNorB(nb);
	   modelMap.addAttribute("message", true);
	   return "forward:/backend/addnewsui";
   }
	
	@RequestMapping("/newsmagui")
	public String newsmag(ModelMap modelMap)
	{
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:newsmag","ViewTitle:新闻公告管理","smenu:newsandbullet","mmenu:newsmag"});				
		
	    List<newsandbulletin> bulletins =newsandbulletService.selectAllBulletin();
		int newsesCount = newsandbulletService.selectAllNewsCount();
	    List<newsandbulletin> newses = newsandbulletService.selectAllNews();
		int bulletinsCount = newsandbulletService.selectAllBulletinCount();
	    modelMap.addAttribute("bulletins",bulletins);
		modelMap.addAttribute("newsesCount", newsesCount);
	    modelMap.addAttribute("newses",newses);
		modelMap.addAttribute("bulletinsCount", bulletinsCount);
		return PathUtil.returnStr(path, modelMap);
		
	}
	@RequestMapping("/deletenbs")
	@ResponseBody
	public String deletenbs(@RequestParam(value="id",required=true) int id)
	{
		logger.info(id);
		int result = newsandbulletService.deleteByPrimaryKey(id);
		if(result == 1)
			return "success";
		return "fail";
	}
	
	@RequestMapping("/newsdetail/{id}")
	public String newsdetail(ModelMap modelMap,@PathVariable("id") int nid)
	{
		
		
		newsandbulletin news = newsandbulletService.selectByPrimaryKey(nid);
		modelMap.addAttribute("news",news);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:NewsDetail","ViewTitle:新闻公告详细","smenu:newsandbullet","mmenu:newsdetail"});
		return PathUtil.returnStr(path, modelMap);
	}
	@RequestMapping("/allnews_{pagenow}")
	public String allnews(ModelMap modelMap,@PathVariable("pagenow")int pagrnow)
	{
		
		path = PathUtil.setPathParams(new String[]{"PackageName:News","ViewName:newses.jsp","ViewTitle:数计要闻","smenu:newes","mmenu:newsdetail"});
		return PathUtil.returnStr(path, modelMap);
	}
	@RequestMapping("/updatenewui/{id}")
	public String updatenewui(@PathVariable("id") int id,ModelMap modelMap)
	{
		newsandbulletin nb = newsandbulletService.selectByPrimaryKey(id);
		modelMap.addAttribute("news", nb);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:updateNews","ViewTitle:修改新闻公告","smenu:newsandbullet","mmenu:updatenews"});
		return PathUtil.returnStr(path, modelMap);
	}
	@RequestMapping("/updatenew/{id}")
	public String updatenew(@PathVariable("id")int id,ModelMap modelMap,newsandbulletin nb,HttpSession session,HttpServletRequest request)
	{
		String path = request.getContextPath();
		String IP = "";
		InetAddress ia = null;
		try {
			ia = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}   
         IP= ia.getHostAddress();//获取计算机IP  
		String basePath = path + "/ueditor/jsp/";
		newsandbulletin nb1 = newsandbulletService.selectByPrimaryKey(id);
		if(nb1.getUrl()==null)
		{
			List<String>filepath = StringUtil.getInstace().getFilepaths();
				String fp1 = "";
				for(String fp : filepath)
				{
					
					if(nb.getContent().contains(fp))
					{
						fp1 =  fp;
						nb.setUrl(basePath + fp1);
						nb.setIndex(1);
						break;
					}else{
						
					}
				}
		}else{
			if(!nb.getContent().contains(nb1.getUrl()) )
			{
				List<String>filepath = StringUtil.getInstace().getFilepaths();
					String fp1 = "";
					for(String fp : filepath)
					{
						
						if(nb.getContent().contains(fp))
						{
							fp1 =  fp;
							nb.setUrl(basePath + fp1);
							nb.setIndex(1);
							break;
						}
					}
					if(fp1.equals(""))
					{
						nb.setUrl("");
						nb.setIndex(0);
					}
					
			}
		}
		nb.setId(id);
		//logger.info(nb.toString());
		User u = (User)session.getAttribute("user");
		nb.setAuthor(u);
	    newsandbulletService.updateNorB(nb);
	   
	    
	    modelMap.addAttribute("success",true);
		return "forward:/backend/newsdetail/"+id;
	}
}
